package com.example.lab1;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	/*
	 * Метка приложения для вывода отладочных сообщений
	 */
	static final String APP_TAG = "BUNDOS-LAB1";

	/*
	 * Кнопка старт
	 */
	Button mStart;
	/*
	 * Количество соединений
	 */
	EditText mConnectionNumber;

	/*
	 * Сообщение приветствия
	 */
	EditText mGreeting;

	/*
	 * Команда клиента
	 */
	EditText mCommand;

	/*
	 * ip адрес сервера
	 */
	EditText mServerIP;

	/*
	 * порт сервера
	 */
	EditText mServerPort;

	/*
	 * Лог для вывода сообщений клиента и сервера
	 */
	public static EditText mLog;

	/*
	 * Сокет
	 */
	Socket mSocket;

	/*
	 * ip адрес сервера
	 */
	private String SERVER_ADDRESS;

	/*
	 * порт сервера
	 */
	public int SERVER_PORT;

	/*
	 * адрес сокета
	 */
	InetSocketAddress mInetSocketAddress;

	/*
	 * Wifi конфиги
	 */
	WifiManager mWifiManager;
	WifiConfiguration config;
	String SSID = "ASUS";
	String netPass = "";
	
	public static Activity mActivity;

	void iniViews() {		
		mConnectionNumber = (EditText) findViewById(R.id.connection_number_edittext);
		mStart = (Button) findViewById(R.id.start_button);
		mGreeting = (EditText) findViewById(R.id.greeting_edittext);
		mCommand = (EditText) findViewById(R.id.command_edittext);
		mServerIP = (EditText) findViewById(R.id.server_ip_edittext);
		mServerPort = (EditText) findViewById(R.id.server_port_edittext);
		mLog = (EditText) findViewById(R.id.log_edittext);
					
		mServerIP.setText("192.168.1.58");
		mServerPort.setText("31007");
		mGreeting.setText("hello");
		mConnectionNumber.setText("10");
		mCommand.setText("sum 50");
		
		mActivity = MainActivity.this;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
		
		iniViews();
		
		OnClickListener buttonsListener = new OnClickListener() {
			@Override
			public void onClick(View v) {

				switch (v.getId()) {
					case (R.id.start_button):
						Thread wifiThread = new Thread(wifiR);
						wifiThread.start();
	
						SERVER_ADDRESS = mServerIP.getText().toString();
						SERVER_PORT = Integer.parseInt(mServerPort.getText().toString());
	
						try {
							InetAddress inetAddress = InetAddress.getByName(SERVER_ADDRESS);
							mInetSocketAddress = new InetSocketAddress(inetAddress, SERVER_PORT);
						}
						catch (IOException e) {
							e.printStackTrace();
						}
	
						String greetingMessage = mGreeting.getText().toString();
						String commandMessage = mCommand.getText().toString();
	
						for (int id = 1; id <= Integer.parseInt(mConnectionNumber.getText().toString()); id++)
							new ClientThread(mInetSocketAddress, id, greetingMessage, commandMessage);					
				}
			}
		};

		mStart.setOnClickListener(buttonsListener);
		
	}

	/*
	 * Описания Wifi соединения
	 */
	private Runnable wifiR = new Runnable() {
		public void run() {
			config = new WifiConfiguration();
			config.SSID = "\"" + SSID + "\"";
			config.preSharedKey = "\"" + netPass + "\"";
			mWifiManager.addNetwork(config);
			mWifiManager.setWifiEnabled(true);
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}